<?php

// Defaults
$Sp_Chr = "";
// File Data
$f = 1;
$log = '';
$handle = fopen($filename, "x");

for ($y = 1; $y <= $GET[0]; $y++) {

    # Trata Nome de Cliente
    /* $CliFirstName = preg_split('/ /',$GET[$y]['CliNombre']);
      if(!isset($CliFirstName[1])) {$CliFirstName[1] = '';}
      $CliLastName = preg_split('/ /',$GET[$y]['CliApellidos']);
      if(!isset($CliLastName[1])) {$CliLastName[1] = '';} */
    $CliFirstName = preg_split('/ /', $GET[$y]['CliNombre']);
    $tCliNames = count($CliFirstName);
    if ($tCliNames < 1) {
        $CliFirstName[1] = '';
    } else {
        $CliFirstName[1] = $CliFirstName[$tCliNames - 1];
    }


    # Trata Nome de Beneficiario
    /* $FirstName = preg_split('/ /',$GET[$y]['BnfNombre']);
      if(!isset($FirstName[1])) {$FirstName[1] = '';}
      $LastName = preg_split('/ /',$GET[$y]['BnfApellidos']);
      if(!isset($LastName[1])) {$LastName[1] = '';} */
    $FirstName = preg_split('/ /', $GET[$y]['BnfNombre']);
    $tNames = count($FirstName);
    if ($tNames < 4) {
        $FirstName[1] = '';
    } else {
        $FirstName[1] = $FirstName[$tNames - 3];
    }
    if ($tNames < 3) {
        $FirstName[2] = '';
    } else {
        $FirstName[2] = $FirstName[$tNames - 1];
    }
    if ($tNames < 1) {
        $FirstName[3] = '';
    } else {
        $FirstName[3] = $FirstName[$tNames - 1];
    }
    if ($GET[$y]['cb_TipoCuenta'] == 'C/C') {
        $GET[$y]['cb_TipoCuenta'] = 0;
    } elseif ($GET[$y]['cb_TipoCuenta'] == 'POUP') {
        $GET[$y]['cb_TipoCuenta'] = 0;
    }


    $log .= str_pad(substr('N', 0, 1), 1, "0", STR_PAD_LEFT) . $Sp_Chr; // X
    $log .= str_pad(substr('CODAGENT', 0, 8), 8, "0", STR_PAD_LEFT) . $Sp_Chr; // X
    $log .= str_pad(substr(str_replace('-', '', $GET[$y]['FraFecha2']), 0, 8), 8, "0", STR_PAD_LEFT) . $Sp_Chr;
    $log .= str_pad(substr(($CNC + $y), 0, 10), 10, "0", STR_PAD_LEFT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['FraReferenciaAg'], 0, 8), 8, "0", STR_PAD_LEFT) . $Sp_Chr;
    $log .= str_pad(substr(number_format($GET[$y]['FraEnviado'], 2, '', ''), 0, 8), 8, "0", STR_PAD_LEFT) . $Sp_Chr;
    $log .= str_pad(substr(number_format($GET[$y]['FraTasa'], 2, '', ''), 0, 8), 8, "0", STR_PAD_LEFT) . $Sp_Chr;
    $log .= str_pad(substr(number_format($GET[$y]['FraComisionCorr'], 2, '', ''), 0, 6), 6, "0", STR_PAD_LEFT) . $Sp_Chr;
    $log .= str_pad(substr(number_format($GET[$y]['FraARecibir'], 2, '', ''), 0, 10), 10, "0", STR_PAD_LEFT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['FraMonDPago'], 0, 3), 3, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['FraMensaje'], 0, 100), 100, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($FirstName[0], 0, 20), 20, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($FirstName[1], 0, 20), 20, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['CliDNI'], 0, 20), 20, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($FirstName[0], 0, 20), 20, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($FirstName[1], 0, 20), 20, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($FirstName[2], 0, 20), 20, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($FirstName[3], 0, 20), 20, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['BnfCedula'], 0, 16), 16, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['BnfDireccion'], 0, 50), 50, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['BnfBarrio'], 0, 50), 50, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['BnfCiudad'], 0, 20), 20, 0, STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['BnfProvincia'], 0, 20), 20, 0, STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['BnfTelefono'], 0, 20), 20, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['BnfPaisCode'], 0, 2), 2, 0, STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['FraModoPago'], 0, 1), 1, 0, STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['bp_IdBanco'], 0, 8), 8, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['bp_Name'], 0, 25), 25, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['cb_Sucursal'], 0, 25), 25, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['cb_TipoCuenta'], 0, 10), 10, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['cb_Cuenta'], 0, 25), 25, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr($GET[$y]['FraAgenciaPago'], 0, 8), 8, " ", STR_PAD_RIGHT) . $Sp_Chr;
    $log .= str_pad(substr('', 0, 16), 16, " ", STR_PAD_RIGHT) . PHP_EOL; # CLAVE
}
fwrite($handle, $log);
fclose($handle);
?>